package collection;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: null
 * @Project: testDemo
 * @Author: hjibo
 * @CreateDate: 2019/1/23 14:25
 * @ModificationHistory: （who whatTime doWhat）
 */
public class ListDemo {

//    public static int getArrayListCapacity(ArrayList<?> arrayList) {
//        Class<ArrayList> arrayListClass = ArrayList.class;
//        try {
//            Field field = arrayListClass.getDeclaredField("elementData");
//            field.setAccessible(true);
//            Object[] objects = (Object[])field.get(arrayList);
//            return objects.length;
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//            return -1;
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//            return -1;
//        }
//    }

/**
 * description: 获取list的容量
 * create_user: hjibo
 * create_date: 2019/1/24 08:35
 * param :
 */
public static int getArrayListCapacity(List arrayList) {
    try {
        Field field = ArrayList.class.getDeclaredField("elementData");
        field.setAccessible(true);
        Object[] objects = (Object[])field.get(arrayList);
        return objects.length;
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
        return -1;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return -1;
    }
}

}
